import Vue from 'vue'
import VueRouter from 'vue-router'
import Cookies from 'js-cookie';
import {login} from "@/api";

Vue.use(VueRouter)

// 定义路由规则数组
const routes = [
    {
        path: '/', component: () => import('@/views/Home.vue')
    }
]

// 创建路由对象
const router = new VueRouter({
    // 路由规则
    routes,
    mode: 'history'
})


// 路由守卫
router.beforeEach(async (to, from, next) => {
    let token = Cookies.get('lc-auth-token')

    // token 不存在跳转到门户登录页
    if (!token) {
        login();
    }

    next();
});

// 导出定义好的路由规则
export default router


